Imports System.Web 'Need this for our HTTPCookie class
Imports System.Web.Security ' Very important namespace for this app. Don't forget it.
Imports Microsoft.VisualBasic
Namespace ASPAuthors.aspnetbyexample.ch10
Public Class Chapter10Login
' Our code behind page will inherit the System's Page object and the aspx
' page which presents our data will inherit the event handlers in this file
Inherits System.Web.UI.Page
' We will capture the events that occur within our aspx page and, well, handle them.
Protected WithEvents txtName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPass As System.Web.UI.WebControls.TextBox
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents txtCookie As System.Web.UI.WebControls.TextBox
Protected WithEvents btnYes As System.Web.UI.WebControls.RadioButton
Protected WithEvents btnNo As System.Web.UI.WebControls.RadioButton
Protected WithEvents lblCookie As System.Web.UI.WebControls.Label
Protected WithEvents lblSessionID As System.Web.UI.WebControls.Label
Protected WithEvents lblCookie2 As System.Web.UI.WebControls.Label
Protected WithEvents txtCookie2 As System.Web.UI.WebControls.TextBox
Protected WithEvents cmdLogin As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblSessionID.Text = "Your Session ID is " & Session.SessionID
Dim objCookie As HttpCookie 'By importing the System.Web namespace you can create an instance of the HTTPCookie Class
objCookie = Request.Cookies("Chapter10")
If Not objCookie Is Nothing Then 'If the value is not null then we can great them accordingly
lblMessage.ForeColor = lblMessage.ForeColor.Black()
lblMessage.Text = "Welcome back, I recognized you by your childish cookie! Please login to view Chapter10 online."
ElseIf Session("YourName") = "" Then
' We're checking the Session Object data to see if that variable it contains any string data.
' If it it is empty it's pretty safe to assume they haven't tried to login yet. I know, it's an ugly little hack.
' I don't even need HTML right now! I just plopped a textbox object on the page.
lblMessage.ForeColor = lblMessage.ForeColor.Black()
lblMessage.Text = "Welcome, please login to view Chapter10 online."
Else
' Ok, something's strange here! They have a string data in their Session("YourName") variable. They must have been
' sent back to us by the
lblMessage.ForeColor = lblMessage.ForeColor.Black()
lblMessage.Text = "Unauthorized Access: I'm sorry " & Session("YourName") & ", please check your username and password and try again."
End If
End Sub
Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
If Len(txtName.Text) <= 0 Then
lblMessage.ForeColor = lblMessage.ForeColor.Red()
lblMessage.Text = "Please enter a username."
Exit Sub
End If
If Len(txtPass.Text) <= 0 Then
lblMessage.ForeColor = lblMessage.ForeColor.Red()
lblMessage.Text = "Please enter a password."
Exit Sub
End If
If btnYes.Checked = True Then
Response.Cookies("Chapter10")("Childish") = txtCookie.Text
Response.Cookies("Chapter10")("Mature") = txtCookie2.Text
Response.Cookies("Chapter10").Expires = "3/3/2039"
End If
If FormsAuthentication.Authenticate(txtName.Text, txtPass.Text) = True Then
' Excellent, they are authorized and authenticated
Session("YourName") = txtName.Text
' Send them to the a Chapter10text page
Response.Redirect("Chapter10Text.aspx?Name=" & txtName.Text)
Else
' Ok, at least we have their name and can create a more personalized message.
lblMessage.ForeColor = lblMessage.ForeColor.Black()
lblMessage.Text = "Unauthorized Access: I'm sorry " & Session("YourName") & ", please check your username and password and try again."
End If
End Sub
' NOTE: By assigning both of these radio buttons a shared group name .NET treat them like a control array
Private Sub btnYes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYes.CheckedChanged
' As soon as they select one of my annoying required radio buttons, I enable the login button
' This is not exactly an ideal example of what we can do using postback features, but
' it should at least spark some good ideas.
cmdLogin.Enabled = False
lblCookie.Visible = True
txtCookie.Visible = True
lblCookie2.Visible = True
txtCookie2.Visible = True
End Sub
Private Sub btnNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.CheckedChanged
' I'll use this event to enable the login button. It's default "enabled"
' property is set to false
cmdLogin.Enabled = True
lblCookie.Visible = False
txtCookie.Visible = False
lblCookie2.Visible = False
txtCookie2.Visible = False
End Sub
Private Sub txtCookie_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCookie.TextChanged
cmdLogin.Enabled = True
End Sub
End Class
End Namespace